﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace School.SystemManage.WebPages.Menu
{
    public partial class MenuList : System.Web.UI.Page
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PBindData();
            }
        }

        private void PBindData()
        {
            lv_pmenu.DataSource = db.SubMenu.Where(s => s.ParentCategory == 0);
            lv_pmenu.DataBind();
        }

        private void SBindData(ListView lv, byte category)
        {
            lv.DataSource = db.SubMenu.Where(s => s.ParentCategory == category);
            lv.DataBind();
        }

        protected void lv_pmenu_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                ListView smenulv = (ListView)e.Item.FindControl("lv_smenu");
                ListViewDataItem dataitem = e.Item as ListViewDataItem;
                byte kid = (byte)lv_pmenu.DataKeys[dataitem.DataItemIndex].Value;

                SBindData(smenulv, kid);
            }
        }

        //父导航
        protected void lv_pmenu_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            lv_pmenu.EditIndex = e.NewEditIndex;
            PBindData();
        }

        protected void lv_pmenu_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            TextBox txbMOrder = lv_pmenu.Items[e.ItemIndex].FindControl("txbOrder") as TextBox;
            TextBox txbUrl = lv_pmenu.Items[e.ItemIndex].FindControl("txbUrl") as TextBox;
            byte kid = (byte)lv_pmenu.DataKeys[e.ItemIndex].Value;

            SubMenu menu = db.SubMenu.FirstOrDefault(s => s.Categories == kid);
            menu.LinkUrl = txbUrl.Text.Trim();
            menu.MOrder = int.Parse(txbMOrder.Text.Trim());

            db.SaveChanges();

            lv_pmenu.EditIndex = -1;
            PBindData();
        }

        protected void lv_pmenu_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            lv_pmenu.EditIndex = -1;
            PBindData();
        }

        //子导航
        protected void smenulv_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            ListView lv = (ListView)sender;
            lv.EditIndex = e.NewEditIndex;
            byte kid = (byte)lv.DataKeys[e.NewEditIndex].Values[1];
            SBindData(lv, kid);
        }

        protected void smenulv_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            TextBox txbMOrder = ((ListView)sender).Items[e.ItemIndex].FindControl("txbOrder") as TextBox;
            TextBox txbUrl = ((ListView)sender).Items[e.ItemIndex].FindControl("txbUrl") as TextBox;
            byte kid = (byte)((ListView)sender).DataKeys[e.ItemIndex].Values[0];

            SubMenu menu = db.SubMenu.FirstOrDefault(s => s.Categories == kid);
            menu.LinkUrl = txbUrl.Text.Trim();
            menu.MOrder = int.Parse(txbMOrder.Text.Trim());

            db.SaveChanges();

            ListView lv = (ListView)sender;
            lv.EditIndex = -1;
            byte cid = (byte)lv.DataKeys[e.ItemIndex].Values[1];
            SBindData(lv, cid);
        }

        protected void smenulv_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            ListView lv = (ListView)sender;
            lv.EditIndex = -1;
            byte cid = (byte)lv.DataKeys[e.ItemIndex].Values[1];
            SBindData(lv, cid);
        }
    }
}
